/*
clsquare - closed loop simulation system
Copyright (c) 2004, Neuroinformatics Group, Prof. Dr. Martin Riedmiller,
University of Osnabrueck

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the <ORGANIZATION> nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE. 
*/

#ifndef _plant_h_
#define _plant_h_

  /** Base class for all plant modules in CLSquare. */
class Plant {
public:
  /** Computes the next state of the environment, given a current state and an action (state transition).  
   * \param state: current state of plant
   * \param action: executed action in current state
   * \param reference_input: an external input for non controllable variables like reference values
   * \param next_state: resulting state after executing action
   * \param reward: reward for state transition  
   * \returns true, if next state is not a terminal state. */ 
     virtual bool next_state(const double *state, const double *action, const double *reference_input, double *successor_state, double &reward);
     virtual bool next_state(const double *state, const double *action, double *successor_state, double &reward);

  /** Computes an observation of a state. 
   * \param state : current state
   * \param reference_input : an external input for non controllable variables like reference values
   * \param observation : observation of current state
   * \return true for success */
  virtual bool get_observation(const double *state, const double *reference_input, double *observation);
  virtual bool get_observation(const double *state, double *observation);

  /** Checks, if plant agrees on initial state of episode. If not, the plant module can 
   *  override the initial state  or reject the initial state by returning false. 
   * \param initial_state: initial state   
   * \returns true, if plant agrees on initial state or overrides the initial state */
  virtual bool check_initial_state(double *initial_state);

  /** Initializes plant.
   * Implement one of the following virtual bool init(...) functions for your plant.
   * For convenience you can use the function that meets your requirements most.
   * Other params are set by default as defined below.
   * \param state_dim: plant returns dimension of state space (state vector)
   * \param observation_dim: plant returns dimension of observation space (observation vector) 
   * \param action_dim: plant returns dimension of action space.
   * \param reference_input_dim: dimension of reference input
   * \param delta_t: plant returns duration of one control cycle [s]
   * \param fname: file, which contains configuration of plant module
   * \return true, for success. */
  virtual bool init(int& state_dim, int& observation_dim, int& action_dim, int& reference_input_dim, double& delta_t, const char* fname=0);
  virtual bool init(int& state_dim, int& observation_dim, int& action_dim, double& delta_t, const char *fname=0);
  virtual bool init(int& state_dim, int& action_dim, double& delta_t, const char *fname=0);
  
  /** Initializes environment by calling the init routines above **/
  bool init_main(int& state_dim, int& observation_dim, int& action_dim, int& reference_input_dim, double& delta_t, const char* fname=0);
  
  /** Terminate plant.
   * \return true for success. */
  virtual bool deinit(){return true;};
  
  /** Notifies that an episode has started. */
  virtual void notify_episode_starts(){return;};

  /** Notifies that a command via pipe has arrived. */
  //  virtual void notfiy_command_string(char* buf){return;};
  virtual void notify_command_string(char* buf);
  
  /** Notifies that an episode has stopped.
   * \param final_state: final state of an episode
   * \param terminal_reward: terminal reward, if final state is a terminal state */
  virtual void notify_episode_stops(const double* final_state, double &terminal_reward){terminal_reward = 0;};
  virtual void notify_episode_stops(const double* final_state, const double* final_action, double &terminal_reward) 
  {return notify_episode_stops(final_state, terminal_reward);};

  /** reads configuration of plant. 
   * \param fname: filename
   * \return true for success */
  bool read_options(const char * fname);
  
  /** virtual destructor is necessary since methods declared virtual */
  virtual ~Plant() {return;};


   protected:
  int state_dim; // dimension of state space
  int observation_dim; // dimension of observation space
  int task_mode; // Every plant provides a collection of tasks, each having an individual reward function

};

#ifdef CLSQUARE
#include "registry.h"
#else
#define REGISTER_PLANT(classname, desc)
#endif

#endif
